import type { FormInstance } from 'ant-design-vue/es';
import type { Ref } from 'vue';

export function useFormValid<T extends Object = any>(formRef: Ref<FormInstance>) {
  async function validForm() {
    if (!formRef) return;
    const data = await formRef.value.validate();
    return data as T;
  }

  return { validForm };
}

export function useFormRules() {
  return reactive({
    username: createRule('请输入账号'),
    password: createRule('请输入密码'),
    captcha: createRule('请输入验证码'),
  });
}

function createRule(message: string) {
  return [
    {
      required: true,
      message,
      trigger: 'blur',
    },
  ];
}
